﻿package uc{
	
	import flash.net.NetConnection;
	import flash.net.Responder;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	public class AMFLoader extends EventDispatcher{
		
		private var connection:NetConnection;
		private var responder:Responder;
		public var data:*;
		
		public function AMFLoader(gateway:String="http://localhost/amfphp/gateway.php"){
			
			// Responder to handle data returned from AMFPHP.
			responder = new Responder(onResult, onFault);
			connection = new NetConnection;
			// Gateway.php url for NetConnection
			connection.connect(gateway);
			
		}
		
		public function call(service_method:String, params:Object=null){
			connection.call(service_method, responder, params);
		}
		
		private function onResult(result:Object):void {
			data = result;
			dispatchEvent(new Event("amfLoadComplete"));
		}
		
		private function onFault(fault:Object):void {
			data = fault;
			dispatchEvent(new Event("amfLoadError"));
		}
		
	}
	
}